﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ccit.Utils;
using Ccit.Message.Organ;
using Ccit.SysOrgan;
using System.Xml;
using Newtonsoft.Json;

namespace Ccit.OA.Scheduler
{
    public partial class scheduler_Search_set : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                _hidSelNodeId.Value = "0";
                tvUser.Attributes.Add("oncheck", "tree_oncheck(this)");
            }
            initializePage();
        }

        private void initializePage()
        { 
            createOrganTree();
            tvUser.ExpandLevel = 1; 
        }
        #region 组织机构
        private void createOrganTree()
        {
            //创建树
            Microsoft.Web.UI.WebControls.TreeNode oRoot = new Microsoft.Web.UI.WebControls.TreeNode();
            createOrganRootNode(ref oRoot);
            tvUser.Nodes.Add(oRoot);
            createOrganSubNodes(ref oRoot);
        }

        //创建跟节点
        private void createOrganRootNode(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            LoginUser login = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
            _hidRootId.Value = login.OrganId;
            oRoot.Text = login.OrganName;
            oRoot.CheckBox = true;
            oRoot.ID = "0_0_dept";
        }

        //创建子节点
        private void createOrganSubNodes(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            Microsoft.Web.UI.WebControls.TreeNode oTreeNode;

            string stairDeptsXML = OrganNET.GetStairDeptByOrganId(_hidRootId.Value);

            SchedulerBLL bll = new SchedulerBLL();

            if (stairDeptsXML != "")
            {
                XmlDocument oDom = new XmlDocument();
                oDom.LoadXml(stairDeptsXML);
                if (oDom.GetElementsByTagName("DEPTS").Count > 0)
                {
                    XmlNode oNodeChild = null;
                    int childCount = Convert.ToInt32(oDom.GetElementsByTagName("DEPT").Count);
                    for (int i = 0; i < childCount; i++)
                    {
                        oTreeNode = new Microsoft.Web.UI.WebControls.TreeNode();
                        oNodeChild = oDom.GetElementsByTagName("DEPT").Item(i);
                        oTreeNode.CheckBox = true;
                        oTreeNode.Text = oNodeChild["DEPTNAME"].InnerText;
                        oTreeNode.ID = oNodeChild["DEPTID"].InnerText + "_0_dept";

                        string deptId = oNodeChild["DEPTID"].InnerText;
                        bool bHaveUser = bll.CheckUserbyDeptId(deptId,"");

                        if (oNodeChild.Attributes["HASCHILD"].InnerText == "1" || bHaveUser == true) //有子节点
                        {
                            Microsoft.Web.UI.WebControls.TreeNode oSubNode = new Microsoft.Web.UI.WebControls.TreeNode();
                            oTreeNode.Nodes.Add(oSubNode);
                        }
                        oRoot.Nodes.Add(oTreeNode);
                    }
                }
            }

            //获取部门下的人员

            List<MsUser> lstUsers = bll.GetUserByDeptId("","");
            foreach (MsUser u in lstUsers)
            {
                oTreeNode = new Microsoft.Web.UI.WebControls.TreeNode();
                oTreeNode.Text = u.userName;
                oTreeNode.ID = u.userId + "_0_user";
                oTreeNode.CheckBox = true; 
                oRoot.Nodes.Add(oTreeNode);
            }

            if (stairDeptsXML != "" || lstUsers.Count > 0)
            {
                _hidHaveChild.Value = "Y";
                _hidNodeType.Value = "dept";
            }
            else
            {
                _hidHaveChild.Value = "N";
                _hidNodeType.Value = "dept";
            }
        }

        /// <summary>
        /// 获组织机构取子节点
        /// </summary>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string GetOrganSubNodes(string updeptId, string nodeIndex, string levelStruts)
        {
            SchedulerBLL bll = new SchedulerBLL();
            bool bHaveUser = bll.CheckUserbyDeptId(updeptId,"");
            List<MsUser> lstUsers = bll.GetUserByDeptId(updeptId,"");

            string subNodeXML = DeptNET.GetSubDeptsXML(updeptId);

            subNodeXML = bll.checkHasUsers(subNodeXML,"");
            return "organ_" + nodeIndex + "_" + subNodeXML + "_" + bHaveUser + "_" + JavaScriptConvert.SerializeObject(lstUsers) + "_" + levelStruts + "_" + updeptId;
        }

        #endregion
    }
}
